﻿using JESAI.HttpRequestMonitor.Core.Abstracts;
using JESAI.HttpRequestMonitor.Storage.SQLServer;
using System;

namespace Microsoft.Extensions.DependencyInjection
{
    public static class ServiceBuildExtensions
    {
        public static IHttpRequestMonitorBuilder UseSQLServerStorage(this IHttpRequestMonitorBuilder builder)
        {
            builder.Services.AddOptions();
            builder.Services.Configure<SQLServerStorageOptions>(builder.Configuration.GetSection("Storage"));

            return builder.UseSQLServerStorageService();
        }

        public static IHttpRequestMonitorBuilder UseSQLServerStorage(this IHttpRequestMonitorBuilder builder, Action<SQLServerStorageOptions> options)
        {
            builder.Services.AddOptions();
            builder.Services.Configure<SQLServerStorageOptions>(options);

            return builder.UseSQLServerStorageService();
        }

        private static IHttpRequestMonitorBuilder UseSQLServerStorageService(this IHttpRequestMonitorBuilder builder)
        {
            builder.Services.AddTransient<IRequestMonitorStorage, SQLServerStorage>();
            builder.Services.AddSingleton<SQLServerConnectionFactory>();
            builder.Services.AddSingleton<IModelCreator, ModelCreator>();
            return builder;
        }

    }
}